package cn.micai.base.concurrent;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/7/5 17:53
 * @Description: 使用客户端加锁实现的"缺少即加入"
 */
public class ListHelper<E> {

    public List<E> list = Collections.synchronizedList(new ArrayList<>());

    public boolean putIfAbsent(E x) {
        synchronized (list) {
            boolean absent = !list.contains(x);
            if (absent) {
                list.add(x);
            }
            return absent;
        }
    }

}
